local jianzhi = fk.CreateSkill {
  name = "jianzhi",
  tags = {Skill.Switch},
}

Fk:loadTranslationTable{
  ["jianzhi"] = "剑制",
  [":jianzhi"] = "转换技，出牌阶段，阳：你可以将弃牌堆中的一张武器牌置入一名其他角色的装备区，然后视为对其使用一张【奇正相生】；阴：你可以重铸一张武器牌以视为使用一张附魔其效果的【杀】。",
  [":jianzhi_yang"] = "转换技，出牌阶段，<font color=\"#E0DB2F\">阳：你可以将弃牌堆中的一张武器牌置入一名其他角色的装备区，然后视为对其使用一张【奇正相生】；</font>" ..
  "阴：你可以重铸一张武器牌以视为使用一张附魔其效果的【杀】。",
  [":jianzhi_yin"] = "转换技，出牌阶段，阳：你可以将弃牌堆中的一张武器牌置入一名其他角色的装备区，然后视为对其使用一张【奇正相生】；"..
  "<font color=\"#E0DB2F\">阴：你可以重铸一张武器牌以视为使用一张附魔其效果的【杀】。</font>",

  ["#jianzhi-yang"] = "剑制：将弃牌堆中的一张武器牌置入一名其他角色的装备区，然后视为对其使用一张【奇正相生】",
  ["#jianzhi-choose"] = "剑制：令 %dest 装备你弃牌堆里的一张武器牌",
  ["#jianzhi-choose2"] = "剑制：重铸一张武器牌",
  ["#jianzhi-choose3"] = "剑制：视为使用一张【杀】",
  ["jianzhi_select"] = "剑制",
  ["jianzhi_sha"] = "剑制",

  ["$jianzhi"] = "I am the bone of my sword.So as I pray,unlimited blade works.",
}

jianzhi:addEffect("active", {
  anim_type = "switch",
  target_num = function(self, player)
    return player:getSwitchSkillState(jianzhi.name, false) == fk.SwitchYang and 1 or 0
  end,
  card_num = function(self, player)
    return player:getSwitchSkillState(jianzhi.name, false) == fk.SwitchYang and 0 or 1
  end,
  prompt = function(self, player)
    return player:getSwitchSkillState(jianzhi.name, false) == fk.SwitchYang and "#jianzhi-yang" or "#jianzhi-choose2"
  end,
  can_use = function(self, player) 
    local can_z = player:getSwitchSkillState(jianzhi.name, false) ~= fk.SwitchYang and player:canUse(Fk:cloneCard("slash")) and table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).sub_type == Card.SubtypeWeapon end)
    local can_yin = player:getSwitchSkillState(jianzhi.name, false) ~= fk.SwitchYang and player:canUse(Fk:cloneCard("slash")) and table.find(player:getCardIds("e"), function(id) return Fk:getCardById(id).sub_type == Card.SubtypeWeapon end)
    local cards = Fk:currentRoom().discard_pile
    local can_yang = player:getSwitchSkillState(jianzhi.name, false) == fk.SwitchYang and table.find(cards, function(id) return Fk:getCardById(id).sub_type == Card.SubtypeWeapon end)
    if can_yin or can_yang or can_z then
      return true
    else return false
    end 
  end,
  card_filter = function(self, player, to_select, selected)
    local cardyang = #selected == 0 and Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon
    return player:getSwitchSkillState(jianzhi.name, false) ~= fk.SwitchYang and cardyang
  end,
    target_filter = function(self, player, to_select, selected, selected_cards)
    if player:getSwitchSkillState(jianzhi.name, false) ~= fk.SwitchYang then
      return false
    end 
    local mubiao_yang = #selected == 0 and to_select ~= player and not table.find(to_select:getCardIds("e"), function(id) return Fk:getCardById(id).sub_type == Card.SubtypeWeapon end)
    return player:getSwitchSkillState(jianzhi.name, false) == fk.SwitchYang and mubiao_yang
  end,
  
    on_use = function(self, room, effect)
    local player = effect.from
    local isYang = player:getSwitchSkillState(jianzhi.name, true) == fk.SwitchYang
    local target = effect.tos[1]
  if isYang then
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "jianzhi_select",
      prompt = "#jianzhi-choose::" .. target.id,
      cancelable = false,
      extra_data = {
        jianzhi_target = target.id,
      }
    })
    if success and dat then
      room:moveCardIntoEquip(target, dat.cards, jianzhi.name)
      room:setPlayerMark(target, "jianzhi_source", effect.from.id)
      room:useVirtualCard("raid_and_frontal_attack", nil, player, target, jianzhi.name)
    end
  end
    if not isYang then
      room:recastCard(effect.cards, effect.from, jianzhi.name)
      if not player.dead then
        room:askToUseVirtualCard(player, {
          name = "slash",
          skill_name = jianzhi.name,
          prompt = "#jianzhi-choose3",
          cancelable = false,
                  extra_data = {
          bypass_times = false,
          extraUse = false,
        },
        })
    end
  end
end,
})

return jianzhi
